import hashlib
import hmac
import time
import base64
from datetime import datetime, timedelta

def md5_encode(text):
    """MD5加密，返回16进制字符串"""
    return hashlib.md5(text.encode()).hexdigest()

def sha256_encode(text):
    """SHA256加密，返回16进制字符串"""
    return hashlib.sha256(text.encode()).hexdigest()

def hmac_sha256_encode(text, key):
    """HMAC-SHA256加密，返回16进制字符串"""
    return hmac.new(key.encode(), text.encode(), hashlib.sha256).hexdigest()

def base64_encode(text):
    """Base64编码"""
    return base64.b64encode(text.encode()).decode()

# 原始值和加密结果
plaintext = "001671"
encrypted = "A9XGGbveFBdsQh0n4EXh"

print(f"测试 '{plaintext}' 到 '{encrypted}' 的时间戳加密方法")

# 尝试MD5
md5_result = md5_encode(plaintext)
print(f"MD5: {md5_result}")
if md5_result.startswith(encrypted) or encrypted.startswith(md5_result):
    print("MD5匹配！")

# 尝试SHA256
sha256_result = sha256_encode(plaintext)
print(f"SHA256: {sha256_result}")
if sha256_result.startswith(encrypted) or encrypted.startswith(sha256_result):
    print("SHA256匹配！")

# 尝试Base64
base64_result = base64_encode(plaintext)
print(f"Base64: {base64_result}")
if base64_result.startswith(encrypted) or encrypted.startswith(base64_result):
    print("Base64匹配！")

# 尝试时间戳相关的加密
print("\n尝试时间戳相关的加密...")

# 当前时间戳
current_timestamp = int(time.time())
print(f"当前时间戳: {current_timestamp}")

# 尝试各种时间戳组合
for days in range(-10, 10):
    timestamp = int((datetime.now() + timedelta(days=days)).timestamp())
    
    # 时间戳 + 原始值
    combined = f"{timestamp}{plaintext}"
    md5_result = md5_encode(combined)[:22]
    if md5_result == encrypted:
        print(f"找到匹配! MD5({combined})[:22] = {encrypted}")
        
    # 原始值 + 时间戳
    combined = f"{plaintext}{timestamp}"
    md5_result = md5_encode(combined)[:22]
    if md5_result == encrypted:
        print(f"找到匹配! MD5({combined})[:22] = {encrypted}")
        
    # 时间戳相关变化
    # 当天日期 YYYYMMDD
    date_str = (datetime.now() + timedelta(days=days)).strftime("%Y%m%d")
    combined = f"{date_str}{plaintext}"
    md5_result = md5_encode(combined)[:22]
    if md5_result == encrypted:
        print(f"找到匹配! MD5({combined})[:22] = {encrypted}")

# 尝试将原始值当作时间戳来处理
try:
    # 尝试将001671解释为从1970年1月1日起的天数
    date = datetime(1970, 1, 1) + timedelta(days=int(plaintext))
    date_str = date.strftime("%Y%m%d")
    print(f"如果001671表示天数: {date_str}")
    
    # 尝试各种格式
    formats = ["%Y%m%d", "%Y-%m-%d", "%d%m%Y", "%m%d%Y"]
    for fmt in formats:
        try:
            date_str = date.strftime(fmt)
            md5_result = md5_encode(date_str)[:22]
            if md5_result == encrypted:
                print(f"找到匹配! MD5({date_str})[:22] = {encrypted}")
        except:
            pass
except:
    pass

# 检查是否可能是某种编码或转换
print("\n检查是否是某种编码或转换...")

# Base64编码然后取部分字符
b64_full = base64_encode(plaintext)
if encrypted in b64_full or b64_full in encrypted:
    print(f"可能是Base64的一部分: {b64_full}")

# 十六进制编码
hex_encode = plaintext.encode().hex()
if encrypted in hex_encode or hex_encode in encrypted:
    print(f"可能是十六进制编码的一部分: {hex_encode}")

# 尝试简单的字符映射或置换
print("\n尝试简单的字符替换...")

# 检查是否符合常见的编码模式
print("\n结论:")
print("尝试了MD5、SHA256、Base64以及时间戳相关的组合方式，未找到精确匹配。")
print("建议：")
print("1. 可能使用了自定义的加密算法或密钥")
print("2. 可能是其他加密方法，如Bcrypt、PBKDF2等")
print("3. 可能涉及多步骤加密（如先MD5再Base64等）")
print("4. 建议检查源代码中是否有其他加密相关的函数") 